﻿Option Explicit On
Option Strict On

Imports Microsoft.Xna.Framework.Input

Public Class monitorClass

    Private intShoulderPressTimer As Integer = 2 'If the shoulder keys are pressed for this long it connects
    Private blnWatchForTrigger As Boolean = True
    Private blnVibrateOnStart As Boolean = True

    Private gamePadMonitorThread As Threading.Thread

    Public Event StartTriggered()
    Public Event triggerFinished(ByVal intPlayerIndex As Integer)


    Public Sub StartMonitoring()

        blnWatchForTrigger = True
        gamePadMonitorThread = New Threading.Thread(AddressOf monitorGamePad)
        gamePadMonitorThread.Start()

    End Sub

    Public Sub StopMonitoring()

        blnWatchForTrigger = False

    End Sub

    Private Sub monitorGamePad()

        Dim sButtonPressTime(4) As DateTime

        Dim gstate As GamePadState

        Dim intGamepadIndex As Integer

        While blnWatchForTrigger = True

            For intGamepadIndex = 0 To 3

                gstate = GamePad.GetState(CType(intGamepadIndex, Microsoft.Xna.Framework.PlayerIndex))

                If gstate.IsConnected Then

                    If gstate.IsButtonDown(Buttons.LeftShoulder) And gstate.IsButtonDown(Buttons.RightShoulder) Then

                        If Not sButtonPressTime(intGamepadIndex).Equals(New DateTime) Then

                            If sButtonPressTime(intGamepadIndex).AddSeconds(intShoulderPressTimer) < Now Then

                                RaiseEvent StartTriggered()

                                If blnVibrateOnStart Then GamePad.SetVibration(CType(intGamepadIndex, Microsoft.Xna.Framework.PlayerIndex), 1, 1)

                                Call StopMonitoring()

                                Exit For

                            End If

                        Else

                            sButtonPressTime(intGamepadIndex) = Now

                        End If

                    Else

                        If Not sButtonPressTime(intGamepadIndex).Equals(New DateTime) Then

                            sButtonPressTime(intGamepadIndex) = New DateTime

                        End If

                    End If

                End If

            Next

            Threading.Thread.Sleep(10)

        End While

        'Great the shoulder buttons have been pressed but have they been released?

        Do

            gstate = GamePad.GetState(CType(intGamepadIndex, Microsoft.Xna.Framework.PlayerIndex))

            If gstate.IsButtonUp(Buttons.LeftShoulder) And gstate.IsButtonUp(Buttons.RightShoulder) Then

                If blnVibrateOnStart Then GamePad.SetVibration(CType(intGamepadIndex, Microsoft.Xna.Framework.PlayerIndex), 0, 0)

                RaiseEvent triggerFinished(intGamepadIndex)

                Exit Do

            End If

            Threading.Thread.Sleep(10)

        Loop


    End Sub

End Class
